package com.example.springbott.service;

import com.example.springbott.bean.MarketAd;
import com.example.springbott.bean.MarketAdExample;
import com.example.springbott.common.NewAd;
import com.example.springbott.common.BasePageInfo;
import com.example.springbott.common.CommonData;
import com.example.springbott.mapper.MarketAdMapper;
import com.example.springbott.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 付常莹
 * @description: TODO
 * @date 2022/11/19 20:48
 */
@Service
@EnableTransactionManagement
public class MarketAdServiceIml implements MarketAdService {
    @Resource
    MarketAdMapper marketAdMapper;

    /**
     * 根据条件查询广告
     *
     * @param name
     * @param sort
     * @param content
     * @param info
     * @return
     */
    @Override
    public CommonData<MarketAd> selectAdList(String name, String sort, String content, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketAdExample adExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = adExample.createCriteria();
        //判断传入的参数是否为空
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(content)) {
            criteria.andContentLike("%" + content + "%");
        }

        List<MarketAd> list = marketAdMapper.selectByExample(adExample);
        PageInfo<MarketAd> pageInfo = new PageInfo<>(list);
        int page = pageInfo.getPageNum();
        int pages = pageInfo.getPages();
        int limit = pageInfo.getPageSize();
        long total = pageInfo.getTotal();
        List<MarketAd> list1 = pageInfo.getList();
        //将查询到的数据进行封装
        CommonData<MarketAd> data = new CommonData<MarketAd>(page, pages, (int) total, limit, list1);
        return data;
    }

    /**
     * 新增广告
     *
     * @param marketAd
     * @return
     */
    @Override
    public MarketAd createAd(MarketAd marketAd) {
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        marketAd.setUpdateTime(date);
        marketAd.setAddTime(date);
        marketAdMapper.insertSelective(marketAd);
        MarketAd data = marketAdMapper.selectByPrimaryKey(marketAd.getId());
        return data;
    }


    /**
     * 修改广告
     *
     * @param marketAd
     * @return
     */
    @Override
    public MarketAd updateAd(MarketAd marketAd) {
        marketAdMapper.updateByPrimaryKey(marketAd);
        MarketAd data = marketAdMapper.selectByPrimaryKey(marketAd.getId());
        return data;
    }

    /**
     * 删除广告
     *
     * @param id
     */
    @Override
    public void deleteAd(Integer id) {
        marketAdMapper.deleteByPrimaryKey(id);
    }
}
